package controller;

import java.util.Collections;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import util.CommonCalls;
import util.HibernateUtil;
import util.QueryBuilder;

import com.opensymphony.xwork2.ActionSupport;

public class SearchController extends ActionSupport implements ServletRequestAware{
	public List<model.ProfileFieldDefinition> FieldDefinitions = CommonCalls.getFieldDefinitions();
	public model.BounBookUser UserSearch = new model.BounBookUser();
	public List<model.ProfileFieldDefinition> FilterList;
	public List<model.BounBookUser> UserList;
	public List<Hashtable<Integer,model.UserProfile>> UserListDetails;
	public int status = 0;
	public int searchRank = 0;
	private static final long serialVersionUID = 1L;
	public String UserType = "'' or 1=1";
	HttpServletRequest request;
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}
	public String execute() throws Exception {
		this.status =0;
		this.UserSearch = new model.BounBookUser();
		return SUCCESS;
	}
	public String BounBookUserSearch() throws Exception{
		this.status = 1;
		this.BounBookUserSearchL(this.UserSearch);
		return SUCCESS;
	}
	private void getFilter(){
		FilterList = new java.util.ArrayList<model.ProfileFieldDefinition>();
		String[] filters = request.getParameterValues("filter");
		if(filters != null){
			for(int a=0;a<filters.length;a++){
				FilterList.add(CommonCalls.getSingleFieldDefinition(filters[a]));
			}
		}
	}
	private void BounBookUserSearchL(model.BounBookUser bbu){
		getFilter();
		String main_query = "from BounBookUser as b where ";
		
		String subquery1 = main_query + "("+ QueryBuilder.Build(bbu.getName(), "", "and", "b", "name","or 1=1") + "and" +
		QueryBuilder.Build(bbu.getLastname(), "", "and", "b", "lastname","or 1=1") + "and" +
		QueryBuilder.Build(bbu.getEmail(), "", "and", "b", "email","or 1=1")
		+ ") and (b.userType = "+this.UserType+") ";
		
		String subquery2 = main_query + "("+  QueryBuilder.Build(bbu.getName(), "", "or", "b", "name","") + "or" +
		QueryBuilder.Build(bbu.getLastname(), "", "or", "b", "lastname","") + "or" +
		QueryBuilder.Build(bbu.getEmail(), "", "or", "b", "email","")
		+ ") and (b.userType = "+this.UserType+") ";
		
		String subquery3 = main_query + "("+  QueryBuilder.Build(bbu.getName(), "%", "and", "b", "name","or 1=1") + "and" +
		QueryBuilder.Build(bbu.getLastname(), "%", "and", "b", "lastname","or 1=1") + "and" +
		QueryBuilder.Build(bbu.getEmail(), "%", "and", "b", "email","or 1=1")
		+ ") and (b.userType = "+this.UserType+") ";
		
		String subquery4 = main_query + "("+  QueryBuilder.Build(bbu.getName(), "%", "or", "b", "name","") + "or" +
		QueryBuilder.Build(bbu.getLastname(), "%", "or", "b", "lastname","") + "or" +
		QueryBuilder.Build(bbu.getEmail(), "%", "or", "b", "email","")
		+ ") and (b.userType = "+this.UserType+") ";
		
		List<model.BounBookUser> sublist0 = HibernateUtil.list(subquery1);
		this.searchRank = 0;
		if(sublist0.size()==0){
			this.searchRank = 1;
			sublist0 = HibernateUtil.list(subquery2);
		}
		if(sublist0.size()==0){
			this.searchRank = 2;
			sublist0 = HibernateUtil.list(subquery3);
		}
		if(sublist0.size() == 0){
			this.searchRank = 3;
			sublist0 = HibernateUtil.list(subquery4);
		}
		if(sublist0.size() == 0){
			this.searchRank = 4;
			sublist0 = Collections.emptyList();
		}
		List <Hashtable <Integer,model.UserProfile>> user_details = new java.util.ArrayList < Hashtable<Integer,model.UserProfile>> ();
		for(int a=0;a<sublist0.size();a++){
			user_details.add(a, CommonCalls.getUserInformation(String.valueOf(sublist0.get(a).getId())));
		}
		this.UserListDetails = user_details;
		this.UserList = sublist0;
		
		return;
	}
}
